<template>
<div id="dz_marketInfo" class="page in " data-title="">    
  
    <div class="divselectBox divselectBox_w" v-if="divselectBrandShow">
			<div class="closebtn" data-showid="divselectBrandShow" @click="closeDivselect">X</div>
			<radio-group @change="itemcenterRadioChange" style=" padding-left:0.6rem;padding-bottom:.4rem" >
				<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in itemcenterList" :key="item.center_id">
					<view>
						<radio :value="item.center_id+''" style="transform:scale(0.7)" :checked="radio_group_flex==1"  />{{item.itemcenter}}
					</view>
                </label>    
			</radio-group>						 	
    </div>

   <div class="container" >       
    <header>
      <a href="javascript:;" class="pa back" @click="ufgoto"></a>
      <p>市场预测表-添加</p>
    </header>  
    
    <div class="price-wrap" style="background: none">
      <div class="configInfo-li addMarket-li">
        <div class="flex-wrap-around config-other">
          <span>机型/主机</span>
          <span>
            <input type="text" v-model="frm.hostmodel" />
          </span>
        </div>
        <div class="flex-wrap-around config-other">
          <span>机型/激光器</span>
          <span>            
            <input type="text" v-model="frm.lasermodel" />
          </span>
        </div>
        <div class="flex-wrap-around config-other">
          <span>合同状态</span>
          <span>
             
            <radio-group @change="radioChange"  > 
            <view>
                <label class="radio"><radio value="0" checked="true" />未签</label>
                   <label class="radio" style="padding-left:1rem;"><radio value="1" />已签</label>
            </view>
            </radio-group>

          </span>
        </div>
        <div class="flex-wrap-around config-other">
          <span>发动时间</span>
          <span>
            <!-- <input type="text" v-model="frm.markte_time" /> -->
             <view class="uni-list-cell-db">
                    <picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
                        <view class="uni-input">{{date}}</view>
                    </picker>
                </view>
          </span>
        </div>
        <div class="flex-wrap-around config-other">
          <span>机型书写</span>
          <span>
            <input type="text" v-model="frm.modelcode" />           
          </span>
        </div>
        <div class="flex-wrap-around config-other">
          <span>项目中心</span>
          <span>
             <input v-model="frm.itemcenter" class="config-sel" ref="refitemcenter"  disabled  placeholder="请选择" @click="divselectShow()" />
          </span>
        </div>
      </div>      
    </div>  
    
     <div class="price-btn-wrap " style="position: fixed;">
       <span class="btn" @click="ufsubmit">添加</span>
     </div>
    
   
   </div>   

  
</div>



</template>

<script>
 
 
var mySelf;
export default {
    data() {
      const currentDate = this.getDate({
		    format: true
		    })
        return {
            frm:{
              hostmodel:'',
              lasermodel:'',
              agreementstatus:'0',
              hostmodel:'',
              markte_time:'',
              modelcode:'',
              itemcenter:'',
              user_id:'',
              itemcenter_id:0, 
            },
			
            date:currentDate,
            itemcenterList:{},
            divselectBrandShow:0,
            radio_group_flex:0,
        }

    },
    computed: {
        startDate() {
            return this.getDate('start');
        },
        endDate() {
            return this.getDate('end');
        }
    },beforeCreate() {
			this.$myUtils.isLoginGotoPage();	
    },
    created() {
      mySelf = this;
    },
    mounted() {	
       	
      this.init();
       
    },
    methods: {
        init:function() {
 //成功
            this.getItemcenterList();      
        },
        getItemcenterList() {
            this.$myUtils.myRequest({
                url: '/itemcenter/index',
                method:'POST',                
              }).then(res => {				 
                  if (res.statusCode==200) {
                      mySelf.itemcenterList=res.data.request;
                  }
              })
        },
        closeDivselect(e) {
          let showId = e.currentTarget.dataset.showid;
          this[showId] = 0 ;
        },
        divselectShow() { 
           this.divselectBrandShow = 1; 		
        },
        itemcenterRadioChange(evt) {
debugger
            let $input;	
            let refkey = 'refitemcenter';
            if (mySelf.$refs[refkey].disabled==true) {
              //谷歌浏览器上运行代码
              $input = mySelf.$refs[refkey].$refs.input;
            } else {
              //手机端运行代码
              $input = mySelf.$refs[refkey] ;
            }
            
            for (const $param in mySelf.itemcenterList) {
                if (mySelf.itemcenterList[$param].center_id==evt.detail.value) {
                    $input.dataset.itemcenter_id = evt.detail.value;
                    mySelf.frm.itemcenter_id = evt.detail.value;
                    mySelf.frm.itemcenter = mySelf.itemcenterList[$param].itemcenter;
                    return false;
                }                
            }

        },
        isbrandsInput:function(refkey) {
            let $input;
            if (mySelf.$refs[refkey][0].disabled==true) {
              //谷歌浏览器上运行代码
              $input = mySelf.$refs[refkey][0].$refs.input;
            } else {
              //手机端运行代码
              $input = mySelf.$refs[refkey][0] ;
            }				
            return $input;
			  },
        radioChange(evt) {
          this.frm.agreementstatus = evt.detail.value 
         
        },
        bindDateChange: function(e) {
            this.date = e.target.value
        },
        getDate(type) {
            const date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();

            if (type === 'start') {
                year = year - 60;
            } else if (type === 'end') {
                year = year + 2;
            }
            month = month > 9 ? month : '0' + month;;
            day = day > 9 ? day : '0' + day;
            return `${year}-${month}-${day}`;
        },
        ufgoto:function() {         
          uni.navigateTo({
            url: '/pages/personal/marketlist'
          });
        },
        ufsubmit() {

          this.frm.markte_time = this.date;
          this.frm.user_id = this.$myUtils.getStorage('userId');
		    // this.frm.agreementstatus = this.$refs['agreementstatus'].value;
          var data = {};
          if (!this.frm.hostmodel) {
              return this.$options.methods.showToast('请输入机型/主机');
          }
          if (!this.frm.lasermodel) {
              return this.$options.methods.showToast('请输入机型/激光器');
          }
          if (!this.frm.markte_time) {
              return this.$options.methods.showToast('请输入发动机时间');
          }
          if (!this.frm.modelcode) {
              return this.$options.methods.showToast('请输入机型书写');
          }
          
          if (!this.frm.itemcenter_id) {
              return this.$options.methods.showToast('请输入项目中心');
          }
          console.log(this.frm);
 
          /////
         
          ///
          this.$myUtils.myRequest({
            url: '/Market/add',
            method:'POST',
            data:this.frm
          }).then(res => {				 
               if (res.statusCode==200) {
                  if (res.data.status==200) {
                      this.$myUtils.successGotoUrl('/pages/personal/marketlist');                                          
                  } else {
                       return uni.showToast({
                        title: res.data.msg || '提交失败！',
                        icon:'none',
                        duration:800
                      });
                  }
                
              } else {
                
                  return uni.showToast({
                    title: res.msg || '提交失败！',
                    icon:'none',
                    duration:800
                  });
              }
      
             

          })





      },
      showToast(_title) {
          return uni.showToast({
                  title: _title,
                  icon:'none',
                  duration:500
                });
         
      },
      ////
       bindPickerChange: function(e) {
            console.log('picker发送选择改变，携带值为', e.target.value)
            this.index = e.target.value
        },
        bindDateChange: function(e) {
            this.frm.markte_time = e.target.value
        },
        bindTimeChange: function(e) {
            this.time = e.target.value
        },
        getDate(type) {
            const date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();

            if (type === 'start') {
                year = year - 60;
            } else if (type === 'end') {
                year = year + 2;
            }
            month = month > 9 ? month : '0' + month;;
            day = day > 9 ? day : '0' + day;
            return `${year}-${month}-${day}`;
        }

    },

}
</script>

 